#-*- encoding:utf-8 -*-

from vialink.django.utils import to_json
from usuarios.decorators import login_required, permission_required

from usuarios.models import Permissao

@permission_required('permissao.autocomplete')
@login_required
def permissoes(request):
    """ Recupera as permissões cadastradas no sistema.
    Obs. usado pelo tokenInput.js
    """
    term = request.GET.get('nome', "")
    permissoes = Permissao.objects.filter(nome__icontains = term).order_by('nome')
    result = [
        {
            'id': p.id,
            'name': p.nome,
        } 
        for p in permissoes
    ]
    
    return to_json(result)


